import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ComponentBasic } from '../app.component';
import { Router, ActivatedRoute } from '@angular/router';
import { LoginUser } from '../../kalend';
import { NzMessageService } from 'ng-zorro-antd';
import { PlatformuserService } from '../service/platformuser.service';
@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss'],
  providers: [PlatformuserService]
})
export class LoginComponent extends ComponentBasic implements OnInit {

  validateForm: FormGroup;
  userName: any;
  password: any;
  constructor(private fb: FormBuilder, private router: Router, private userService: PlatformuserService, private activatedRoute: ActivatedRoute, private msg: NzMessageService) {
    super();
    super.setLayout(false, false);
  }

  submitForm(): void {
    for (const i in this.validateForm.controls) {
      if (i) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    }
  }

  /** * 登陆**/
  private logined = (value) => {
    let navurl = '/manage/userlist';
    if (value) {
      if ((value.roleName as Array<any>).findIndex((a) => { return a === '系统管理员' }) > -1) {

      } else if ((value.roleName as Array<any>).findIndex((a) => { return a.indexOf('餐厅') > -1; }) > -1) {
        navurl = '/manage/merchantorderrecord';
      } else if ((value.roleName as Array<any>).findIndex((a) => { return a.indexOf('宣传') > -1; }) > -1) {
        navurl = '/manage/agentmerchantlist';
      }
    }
    // const params = this.activatedRoute.snapshot.params;
    // if (params && params['url']) {
    //   navurl = decodeURIComponent(params['url']);
    // }
    this.router.navigate([navurl]);
    LoginUser.login(value);
  }

  login(name?: any) {
    if (this.userName !== '' && this.password !== '') {
      this.userService.login(this.userName, this.password).then(this.logined);
    } else {
      this.msg.warning('手机号密码不能为空！');
    }
  }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      userName: [null, [Validators.required]],
      password: [null, [Validators.required]],
      remember: [true]
    });
  }

}
